﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using Common;

public partial class dialog_frmuserlist : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        switch (RequestBase.GetFormString("otype"))
        {
            case "loaddata":
                LoadingData();
                break;
        }
    }
    #region 信息绑定
    void LoadingData()
    {
        string SQL = " AND IsLockedOut = 0 AND DeletionStateCode = 0 AND UserName <> 'admin'";

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("searchkey")))
        {
            SQL += " AND FullName LIKE '%" + RequestBase.GetFormString("searchkey") + "%'";
        }

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("omodle")))
        {
            string arrRoleId = "";
            DataTable datRoles = BLL.CommonBLL.Select("System_Roles", "RoleId", " AND IsLockout = 0");
            switch (RequestBase.GetFormString("omodle").ToLower())
            { 
                case "perusalgroup":
                    foreach (DataRow dr in datRoles.Select(""))
                    {
                        if (Validate("received doc", 5, dr[0].ToString().Trim()))
                            arrRoleId += "'" + dr[0].ToString() + "',";
                    }
                    datRoles.Dispose();

                    if (arrRoleId != "")
                    {
                        arrRoleId += "|";
                        arrRoleId = arrRoleId.Replace(",|", "");

                        SQL += " AND SystemUserId IN (SELECT SystemUserId FROM System_UsersInRoles WHERE RoleId IN (" + arrRoleId + "))";
                    }
                    else { SQL += " AND 1 <> 1"; }
                    break;
                case "approvegroup":
                    foreach (DataRow dr in datRoles.Select(""))
                    {
                        if (Validate("received doc", 3, dr[0].ToString().Trim()))
                            arrRoleId += "'" + dr[0].ToString() + "',";
                    }
                    datRoles.Dispose();

                    if (arrRoleId != "")
                    {
                        arrRoleId += "|";
                        arrRoleId = arrRoleId.Replace(",|", "");

                        SQL += " AND SystemUserId IN (SELECT SystemUserId FROM System_UsersInRoles WHERE RoleId IN (" + arrRoleId + "))";
                    }
                    else { SQL += " AND 1 <> 1"; }
                    break;
            }
        }
        

        string ctrl = "~/admin/dialog/_controls/ctlsystemuserlist.ascx";
        Control uc_rep = LoadControl(ctrl);

        Repeater rptUserlistForListbox = (Repeater)uc_rep.FindControl("rptUserlistForListbox");

        try
        {
            DataTable dat = BLL.CommonBLL.Select("System_UserBase", "*", SQL + "  ORDER BY NickName ASC");
            rptUserlistForListbox.DataSource = dat;
            rptUserlistForListbox.DataBind();

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            rptUserlistForListbox.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
    #endregion
}
